package cn.moukou.sbzz.controller;

import cn.moukou.sbzz.pojo.Bank;
import cn.moukou.sbzz.service.AdminService;
import cn.moukou.sbzz.service.BankService;
import cn.moukou.sbzz.service.EnterpriseService;
import cn.moukou.sbzz.util.JwtUtil;
import cn.moukou.sbzz.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sbzz/api")
public class BankController {

    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    BankService bankService;
    @Autowired
    AdminService adminService;

    @GetMapping("/bank/list")
    public Resp<List<Bank>> getBankList() {
        List<Bank> bankList = bankService.getBankList();
        return Resp.ok(bankList);
    }

    @GetMapping("/bank/one")
    public Resp<Bank> getOneBank(@RequestParam("id") String id) {
        return Resp.ok(bankService.getOneBank(id));
    }

    @GetMapping("/bank/my")
    public Resp<Bank> getMyBank(HttpServletRequest request) throws Exception {
        return Resp.ok(bankService.getOneBank(JwtUtil.getUid(request)));
    }

    @GetMapping("/bank/to-auth")
    public Resp<List<Bank>> getBankToAuth() throws Exception {
        return Resp.ok(bankService.getBankToAuth());
    }

    @PostMapping("/bank/auth")
    public Resp<String> authBank(@RequestParam("id") String id){
        if(bankService.authBank(id) > 0){
            return Resp.ok("认证成功");
        } else {
            return Resp.fail("认证失败");
        }
    }

    @PostMapping("/bank/submit")
    public Resp<String> submitBank(@RequestParam("id") String id, @RequestParam("name") String name,
                                 @RequestParam("detail") String detail, @RequestParam("link") String link){
        if(bankService.submitBank(id, name, detail, link) > 0){
            return Resp.ok("提交成功");
        } else {
            return Resp.fail("提交失败");
        }
    }

    @PostMapping("/bank/access")
    public Resp<String> accessBank(@RequestParam("id") String id, @RequestParam("form") String form,
                                   @RequestParam("business") String business, @RequestParam("pool") String pool){
        if(bankService.accessBank(id, form, business, pool) > 0){
            return Resp.ok("认证成功");
        } else {
            return Resp.fail("认证失败");
        }
    }

}
